package com.hoapital.outpbillwork.billWorkHandler;

import com.hoapital.outpbillwork.module.controller.vo.ApplyInfo;
import com.hoapital.outpbillwork.module.controller.vo.ItemData;
import com.hoapital.outpbillwork.module.controller.vo.OrderInfo;
import com.hoapital.outpbillwork.module.entity.*;
import com.hoapital.outpbillwork.module.service.OutpDoctorAssistantService;
import com.hospital.common.core.constants.AllFinalValueUtils;
import com.hospital.common.core.exception.ExceptionHelp;
import com.hospital.common.core.exception.ExceptionHelpEnum;
import com.hospital.common.core.hl7Model.hospitalWork.BATCH;
import com.hoapital.outpbillwork.function.setValue.SetFunctionHL7;
import com.hospital.common.core.module.controller.vo.CancelOrder;
import com.hospital.common.core.module.controller.vo.OrderContent;
import com.hospital.common.core.module.controller.vo.Receive;
import com.hoapital.outpbillwork.module.mapper.LabTestMasterMapper;
import com.hospital.common.core.result.SuccessTip;
import com.hospital.common.core.result.Tip;
import com.hospital.common.core.utils.DateUtil;
import com.hospital.common.core.utils.SpringContextHolder;
import com.hospital.common.core.utils.XMLUtil;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lvyao
 * @description 除特殊门诊开单项目(特殊指检查检验 、 处方 、 手术等)的开单处理类
 * @date 2022/3/1
 */

@Transactional(rollbackFor = Exception.class)
public class OtherTypeOrderBusiness extends AbstractOrderWorkBusiness {
    private OutpDoctorAssistantService outpDoctorAssistantService = SpringContextHolder.getBean(OutpDoctorAssistantService.class);
    private LabTestMasterMapper labTestMasterMapper = SpringContextHolder.getBean(LabTestMasterMapper.class);

    @Override
    public ExceptionHelp checkOtherOrder(OrderInfo orderInfo) throws ExceptionHelp {
        return null;
    }

    @Override
    public ExceptionHelp checkOtherExpecOrder(ApplyInfo orderInfo) throws ExceptionHelp {
        return null;
    }

    @Override
    public String getOrderNotemp(OrderContent orderContent, Timestamp visitDate, int visitNo) throws ExceptionHelp {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        String billNumber = df.format(visitDate) + visitNo;
        return billNumber;
    }

    @Override
    protected Object doOtherInfo(List<ItemData> itemDataList, String testNo, PatientInfo patientInfo, OrderContent order, int age, String testCause, String clininDiag, Timestamp visitDate, int visitNo, String subject, int outpId) throws ExceptionHelp {
        return null;
    }

    @Override
    protected Object doOtherInfoDeatil(String orderNo, int itemNo, String itemCode, String itemName, BigDecimal price, BigDecimal charges) {
        return null;
    }

    @Override
    protected BATCH packageMessage(OutpMedrec outpMedrec, List<OutpOrderDescTemp> outpOrderDescTempList,
                                   List<Object> objectMaster, List<Object> objectDetail,
                                   OrderInfo orderInfo, PatientInfo patientInfo, String orc1, String orc5) throws ExceptionHelp {
        return SetFunctionHL7.packageOmallMessage(outpMedrec, outpOrderDescTempList, null, null,
                orderInfo.getHospitalCode(), orderInfo.getHospitalName(), orderInfo.getOutpOrInp(), patientInfo,
                orc1, orc5, labTestMasterMapper, AllFinalValueUtils.ADT, AllFinalValueUtils.MESSAGE_FROM_PUBLIC,
                AllFinalValueUtils.MESSAGE_EVENT_PAT_VISIT, AllFinalValueUtils.MESSAGE_EVENT_NAME_PAT_VISIT);
    }


    @Override
    public Object doOtherBillDetail(String testNo, int outpId, Timestamp visitDate, int visitNo, int itemNo, ClinicItemBill clinicItemBill,
                                    ItemData itemData, OrderContent orderContent, String patientId) throws ExceptionHelp {
        return null;
    }

    @Override
    public Object doAfterData(String type, Object object, BigDecimal costs, BigDecimal charges) throws ExceptionHelp {
        return null;
    }

    @Override
    public String getReplaceString(BATCH getHl7LabMessage) throws ExceptionHelp {
        String utf8 = XMLUtil.convertToXml(getHl7LabMessage).
                replace(AllFinalValueUtils.UNICODE_DEFAULT_TARGET, AllFinalValueUtils.UNICODE_DEFAULT_REPLACEMENT)
                .replace("<type>ADT</type>", "");
        return utf8;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateOtherTable(ClinicMaster clinicMaster, OutpMedrec outpMedrec, List<OutpOrderDescTemp> outpOrderDescTempList, List<OutpBillItemsTemp> outpBillItemsTempList,
                                    List<OutpDoctorAssistant> outpDoctorAssistantList,
                                    List<Object> objectMasterList, List<Object> objectItemsList, List<Object> objectBillitemsList,
                                    OutpPrepayRcptTemp outpPrepayRcptTemp, List<OrderVsBillno> orderVsBillnoList) throws ExceptionHelp {
        return outpDoctorAssistantService.updateAllTab(AllFinalValueUtils.MATERIAL, clinicMaster, outpMedrec, outpOrderDescTempList, outpBillItemsTempList, outpDoctorAssistantList, objectMasterList, objectItemsList, objectBillitemsList, outpPrepayRcptTemp, orderVsBillnoList);
    }

    @Override
    public Tip billWork(Receive receive, HttpServletRequest request) throws ExceptionHelp {
        return null;
    }

    @Override
    public Tip orderCancel(CancelOrder CancelOrder, HttpServletRequest request) throws ExceptionHelp {
        return null;
    }

    @Override
    public Tip sendApplyHl7File(OrderInfo orderInfo, String oneKey, String msgType) throws ExceptionHelp {
        Map<String, Object> hl7ApplyBaseData = getHl7ApplyBaseData(oneKey, orderInfo.getPatInfo().getPatientId(), orderInfo.getVisitId());
        List<OutpOrderDescTemp> outpOrderDescTempList = new ArrayList<>();
        for (ApplyInfo applyInfo : orderInfo.getApplyInfo()) {

            BATCH hl7message = packageMessage((OutpMedrec) hl7ApplyBaseData.get("outpMedrec"), outpOrderDescTempList,
                    null, null,
                    orderInfo, (PatientInfo) hl7ApplyBaseData.get("patientInfo"), "NW", "SC");
            //xml申请文件保存并推送
            oneKey = DateUtil.formatDate(DateUtil.parseTime(applyInfo.getOrder().getOrderDate()), "yyyyMMddHHmmss") + applyInfo.getOrder().getOrderNoumber();
            boolean push = saveApplyHl7message(orderInfo.getHospitalCode(), orderInfo.getHospitalName(), oneKey, hl7message, msgType);
            if (!push) {
                throw new ExceptionHelp(ExceptionHelpEnum.SEND_APPLY_MSG_FILAD);
            }
        }
        return new SuccessTip();
    }
}
